package com.cpd.blog.service.impl;

import com.cpd.blog.common.constant.Constant;
import com.cpd.blog.common.constant.RedisKey;
import com.cpd.blog.common.constant.StatusCode;
import com.cpd.blog.common.utils.CheckUtil;
import com.cpd.blog.common.utils.GenerateUtil;
import com.cpd.blog.common.utils.RedisUtil;
import com.cpd.blog.dto.Result;
import com.cpd.blog.dto.User.LoginReq;
import com.cpd.blog.dto.User.RegisterReq;
import com.cpd.blog.entity.User;
import com.cpd.blog.service.LoginService;
import com.cpd.blog.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Random;

/**
 * @author Copydown
 * @date 2021/8/16 20:00
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {

    private final UserService userService;

    private final RedisUtil redisUtil;

    @Override
    public Result login(LoginReq loginReq) {

        String account = loginReq.getAccount();
        String password = loginReq.getPassword();

        if (CheckUtil.hasBlank(account, password)) {
            return Result.fail(StatusCode.PARAMS_NULL);
        }
        User user = userService.findUserByAccount(account);
        if (CheckUtil.isNull(user)) {
            return Result.fail(StatusCode.ACCOUNT_NO_EXIST);
        }
        if (CheckUtil.isFalse(userService.checkUserPwd(user, password))) {
            return Result.fail(StatusCode.USER_PASSWORD_WRONG);
        }

        return Result.success(userService.updateUserLogin(user));
    }


    @Override
    public Result logout(String token) {
        redisUtil.delete(RedisKey.TOKEN + token);
        return Result.success(StatusCode.USER_LOGOUT_SUCCESS);
    }

    @Override
    public Result register(RegisterReq registerReq) {

        String account = registerReq.getAccount();
        String password = registerReq.getPassword();
        String email = registerReq.getEmail();

        if (CheckUtil.isNotNull(userService.findUserByAccount(account))) {
            return Result.fail(StatusCode.ACCOUNT_EXIST);
        }
        User user = new User();
        Random random = new Random();
        LocalDateTime now = LocalDateTime.now();
        user.setNickname(Constant.DEFAULT_USER_NAME + GenerateUtil.genUuidSize6());
        user.setAccount(account);
        String salt = GenerateUtil.genSaltSize5();
        user.setSalt(salt);
        user.setPassword(DigestUtils.md5Hex(password + salt));
        user.setEmail(email);
        user.setCreateTime(now);
        user.setLastLogin(now);
        int index = random.nextInt(6) + 1;
        String avatarPath = "/static/user/user_" + index + Constant.PNG;
        user.setAvatar(avatarPath);
        user.setVersion(Constant.ZERO_LONG);
        user.setAdmin(Constant.BOOLEAN_FALSE);
        user.setDeleted(Constant.BOOLEAN_FALSE);
        user.setStatus(Constant.USER_STATUS_DEFAULT);
        userService.save(user);

        String token = userService.updateUserLogin(user);
        return Result.success(token);
    }
}
